<?php

namespace App\Http\Requests\Project;

use App\Models\PmProjectBasic;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Validation\Rule;

class PmProjectRequest extends FormRequest
{
    /**
     * 表示验证器是否应在第一个规则失败时停止
     *
     * @var bool
     */
    protected $stopOnFirstFailure = true;

    /**
     * Determine if the user is authorized to make this request.
     */
    public function authorize(): bool
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array|string>
     */
    public function rules(): array
    {
        $basicRules = [];
        if ($this->route('id')) {
            /* update data */
            $basicRules['project_name'] = ['required', Rule::unique('App\Models\PmProjectBasic')->ignore($this->route('id'))];
        } else {
            /* store data */
            $basicRules['project_name'] = ['required', Rule::unique('App\Models\PmProjectBasic')->where('deleted_at')];
        }

        $mainRules = [
            'foundation' => 'required',
            'field' => 'required',
            'category' => 'required',
            'carry_out_period' => 'required',
            'carry_out_area' => 'required',
            'benefit_group' => 'required',
            'direct_donate_budget' => 'required',
            'activity_cost_budget' => 'required',
            'charger' => 'required',
            'benefit_group_expectation' => 'required',
            'project_goal' => 'required',
            'project_summary' => 'required',
            'project_key_activity' => 'required',
            'project_output' => 'required',
            'project_step' => 'required|integer',
            'submit_type' => 'required',
        ];

        $projectIdRules = [
            'project_id' => ['integer', 'min:1', Rule::exists('App\Models\PmProjectBasic', 'id')],
        ];

        /* 下一步保存 */
        if ($this->submit_type == PmProjectBasic::SUBMIT_NEXT_STEP) {
            if ($this->project_step == 1) {
                return array_merge($basicRules, $mainRules);
            }
        }

        $fileRules = [
            'files.council_resolution' => 'required',
        ];
        /* 提交审批 */
        if ($this->submit_type == PmProjectBasic::SUBMIT_SAVE) {
            return array_merge($basicRules, $mainRules, $projectIdRules, $fileRules);
        }

        return $basicRules;
    }

    /**
     * 获取已定义验证规则的错误消息
     *
     * @return string[]
     */
    public function messages()
    {
        return [
            'project_name.required' => '项目名称不能为空',
            'project_name.unique' => '项目名称已存在',
            'project_id.integer' => '项目ID必须为整数',
            'project_id.exists' => '项目不存在',
            'project_id.min' => '项目信息错误',
            'foundation.required' => '基金会不能为空',
            'field.required' => '项目所属领域不能为空',
            'category.required' => '项目所属类别不能为空',
            'carry_out_period.required' => '实施期限不能为空',
            'carry_out_area.required' => '实施区域不能为空',
            'benefit_group.required' => '受益群体不能为空',
            'direct_donate_budget.required' => '直接捐赠预算不能为空',
            'activity_cost_budget.required' => '活动成本预算不能为空',
            'charger.required' => '项目负责人不能为空',
            'benefit_group_expectation.required' => '受益人期望变化不能为空',
            'project_goal.required' => '项目目标不能为空',
            'project_summary.required' => '项目概述不能为空',
            'project_key_activity.required' => '项目关键活动不能为空',
            'project_output.required' => '项目产出不能为空',
            'files.council_resolution.required' => '理事会决议附件文件不能为空',
            'step.required' => '请告诉我你保存的是第几步呀',
            'submit_type.required' => '保存类型不能为空',
        ];
    }
}
